<template>
	<view>
		<!-- <view>增加/修改企业</view> -->
		<view class="form-item cu-form-group">
			<view class="title">* 驻企企业</view>
			<picker @change="PickerChange1" :value="index_company" :range="companyArray" range-key="name">
				<view class="picker">
					{{pick1Change}}
				</view>
			</picker>
		</view>
		<view class="form-item cu-form-group">
			<view class="title">* 驻企人员</view>
			<picker @change="PickerChange" :value="index" :range="picker" range-key="name">
				<view class="picker">
					{{pickChange}}
				</view>
			</picker>
		</view>

		<view class="yes" @tap="handleClickYes">确定</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				token: '',
				index: 0,
				index_company: 0,
				picker: [],
				pickerName: '',
				companyArray: [],
				companyArrayName: '',
				user_id: '', //驻企人员id
				station_id: '', //企业id
				changeId: '', //修改用的id
				msg: '', //错误提示
			}
		},
		onLoad(option) {
			if (option.items) {
				let list = JSON.parse(option.items)
				// console.log(list)
				this.changeId = list.id;
				this.user_id = list.user_id;
				this.station_id = list.company_id;
				this.pickerName = list.user_name;
				this.companyArrayName = list.company_name;
			}
		},
		computed: {
			pick1Change() {
				if (this.companyArrayName) {
					return this.companyArrayName
				} else {
					return this.index_company > 0 ? this.companyArray[this.index_company].name : '请选择'
				}
			},
			pickChange() {
				if (this.pickerName) {
					return this.pickerName
				} else {
					return this.index > 0 ? this.picker[this.index].name : '请选择'
				}
			},
		},
		methods: {
			// 取token
			getToken() {
				try {
					const value = uni.getStorageSync('login_key');
					if (value) {
						this.token = value;
					}
				} catch (e) {
					// error
				}
			},
			// 选择
			PickerChange1(e) {
				this.companyArrayName = ''
				uni.hideKeyboard();
				this.index_company = e.detail.value
				this.station_id = this.companyArray[this.index_company].id
			},
			PickerChange(e) {
				this.pickerName = '';
				uni.hideKeyboard();
				this.index = e.detail.value
				this.user_id = this.picker[this.index].id
			},
			// 请求用户
			async getUser() {
				this.getToken()
				// 发请求
				let url = this.ServicesApi.userList;
				let data = {

				};
				let result = await this.Services.post(url, data);
				if (result.code == this.StatusCode.SUCCESS_CODE) {
					// console.log(res.data.data)
					this.picker = result.data;
					this.index = this.picker.findIndex(i => i.id == this.user_id) > -1 ? this.picker.findIndex(i => i.id == this.user_id) : 0;
					console.log('this.index---->', this.index, this.user_id);
				}

			},
			async getStation() {
				this.getToken()
				// 发请求
				let url = this.ServicesApi.companyList;
				let data = {

				};
				let result = await this.Services.post(url, data);
				if (result.code == this.StatusCode.SUCCESS_CODE) {
					// console.log(res.data.data)
					this.companyArray = result.data;
					this.index_company = this.companyArray.findIndex(i => i.id == this.station_id) > -1 ? this.companyArray.findIndex(i => i.id ==
						this.station_id) : 0;
					console.log('this.index_company---->', this.index_company, this.station_id);
				}

			},
			async handleClickYes() {
				var pages = getCurrentPages();
				var currPage = pages[pages.length - 1]; //当前页面
				var prevPage = pages[pages.length - 2]; //上一个页面
				//直接调用上一个页面的setData()方法，把数据存到上一个页面中去
				prevPage && prevPage.setData && prevPage.setData({
					isDoRefresh: true
				});
				this.getToken()
				// 发请求
				let url = this.ServicesApi.editStation;
				let data = {
					user_id: this.user_id, //搜索用户id 用户列表接口
					company_id: this.station_id, //搜索企业id 企业列表接口
					id: this.changeId || '', //驻企id 有就是修改，没有就是新增
				};
				
				if (!this.station_id) {
					return uni.showToast({
						icon: 'none',
						title: '请选择驻企企业'
					});
				}
				if (!this.user_id) {
					return uni.showToast({
						icon: 'none',
						title: '请选择驻企人员'
					});
				}
				
				let result = await this.Services.post(url, data);
				if (result.code == this.StatusCode.SUCCESS_CODE) {
					uni.showToast({
						icon: 'none',
						title: result.msg,
					});
					setTimeout(() => {
						uni.navigateBack({
							delta: 1,
							animationType: 'pop-out',
							animationDuration: 200
						});
					}, 1500);
				} else {
					uni.showToast({
						icon: 'none',
						title: result.msg,
					});
				}
				// uni.request({
				// 	url: this.ServicesApi.editStation,
				// 	method: 'POST',
				// 	header: {

				// 		"content-type": "application/x-www-form-urlencoded"
				// 	},
				// 	data: {
				// 		token: this.token,
				// 		user_id: this.user_id, //搜索用户id 用户列表接口
				// 		company_id: this.station_id, //搜索企业id 企业列表接口
				// 		id: this.changeId || '', //驻企id 有就是修改，没有就是新增
				// 	},
				// 	success: (res) => {
				// 		if (res.data.code == 0) {
				// 			uni.showToast({
				// 				icon: 'none',
				// 				title: res.data.msg,
				// 			});
				// 			return false
				// 		} else {
				// 			// uni.navigateTo({
				// 			// 	url:'./stationManage'
				// 			// })
				// 			uni.navigateBack({
				// 				delta: 1,
				// 				animationType: 'pop-out',
				// 				animationDuration: 200
				// 			});
				// 			uni.showToast({
				// 				title: res.data.msg
				// 			});
				// 		}
				// 	}
				// });
			}
		},
		mounted() {
			this.getUser()
			this.getStation()
		}
	}
</script>

<style scoped lang="stylus">
	.msg {
		height: 30upx;
		font-size: 28upx;
		color: red;
	}

	.yes {
		width: 440upx;
		height: 76upx;
		background: linear-gradient(270deg, rgba(0, 106, 231, 1) 0%, rgba(66, 157, 255, 1) 100%);
		border-radius: 4upx;
		text-align: center;
		line-height: 76upx;
		margin: 400upx auto;
		color: #fff;
		font-size: 32upx;
	}
</style>
